package com.zzyl.serve.service.impl;

import java.beans.Transient;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 护理计划Service业务层处理
 *
 * @author laoYe
 * @date 2025-06-15
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlanVo vo = new NursingPlanVo();
        NursingPlan plan = getById(id);
//        vo.setId(plan.getId());
//        vo.setPlanName(plan.getPlanName());
        BeanUtils.copyProperties(plan,vo);

        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.selectNursingProjectPlanListByPlanId(id);
        vo.setProjectPlans(projectPlans);
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transient
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setPlanName(nursingPlanDto.getPlanName());
        nursingPlan.setStatus(nursingPlanDto.getStatus());
        nursingPlan.setSortNo(nursingPlanDto.getSortNo());
        save(nursingPlan);

        Long planId = nursingPlan.getId();
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        // 修复逻辑错误：改为 !projectPlans.isEmpty()
        if (projectPlans != null && !projectPlans.isEmpty()) {
            // 为每个项目计划设置 planId
            for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId(planId);
            }
            // 批量插入应该在循环外执行一次
            nursingProjectPlanMapper.insertBatch(projectPlans);
        }

        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional//事务管理
    public int updateNursingPlan(NursingPlanDto nursingPlanDto){

            // 属性拷贝
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
            Long planId = nursingPlan.getId();
            // 删除护理计划对应的护理项目列表
            nursingProjectPlanMapper.deleteByPlanId(planId);

            // 提交过来的护理项目集合
            List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

            // 判断dto中的项目列表为空，如果不为空则要批量添加
            if (!CollectionUtils.isEmpty(projectPlans)) {
                // 补全护理计划id
                projectPlans.forEach(projectPlan -> projectPlan.setPlanId(planId));
                // 批量保存护理计划对应的护理项目列表
                nursingProjectPlanMapper.insertBatch(projectPlans);
            }

            // 不管项目列表是否为空，都要修改护理计划
            return nursingPlanMapper.updateById(nursingPlan);

    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return nursingPlanMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return nursingPlanMapper.deleteById(id);
    }
}
